package com.coisini.controller.admin;

import com.coisini.dto.*;
import com.coisini.result.PageResult;
import com.coisini.result.Result;
import com.coisini.service.OrderService;
import com.coisini.vo.OrderStatisticsVO;
import com.coisini.vo.OrderVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@Slf4j
@RequestMapping("/admin/order")
@Api(tags = "管理端-订单接口")
public class OrderController {

    @Autowired
    private OrderService orderService;

    /**
     * 1.订单搜索，分页查询
     * @param ordersPageQueryDTO
     * @return
     */
    @ApiOperation("订单搜索")
    @GetMapping("/conditionSearch")
    public Result<PageResult> pageQuery(OrdersPageQueryDTO ordersPageQueryDTO){
        log.info("订单分页查询：{}",ordersPageQueryDTO);
        PageResult pageResult = orderService.conditionSearch(ordersPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 2.查询订单详情
     * @param id
     * @return
     */
    @ApiOperation("查询订单详情")
    @GetMapping("/details/{id}")
    public Result<OrderVO> detail(@PathVariable("id") Long id){
        log.info("查询订单详情：{}",id);
        OrderVO orderVO=orderService.getById(id);
        return Result.success(orderVO);
    }

    /**
     * 3.取消订单
     * @param ordersCancelDTO
     * @return
     * @throws Exception
     */
    @ApiOperation("取消订单")
    @PutMapping("/cancel")
    public Result cancel(@RequestBody OrdersCancelDTO ordersCancelDTO) throws Exception {
        log.info("取消订单：{}",ordersCancelDTO);
        orderService.cancel(ordersCancelDTO.getId());
        return Result.success();
    }

    /**
     * 4.获取各个状态订单的数量
     * @return
     */
    @ApiOperation("获取各个状态订单的数量")
    @GetMapping("statistics")
    public Result<OrderStatisticsVO> statistics(){
        log.info("获取各个状态订单的数量");
        OrderStatisticsVO orderStatisticsVO=orderService.statistics();
        return Result.success(orderStatisticsVO);
    }

    /**
     * 5.商家接单
     * @return
     */
    @ApiOperation("商家接单")
    @PutMapping("/confirm")
    public Result confirm(@RequestBody OrdersConfirmDTO ordersConfirmDTO){
        log.info("商家接单:{}",ordersConfirmDTO);
        orderService.confirm(ordersConfirmDTO);
        return Result.success();
    }

    /**
     * 6.派送订单
     * @param id
     * @return
     */
    @ApiOperation("派送订单")
    @PutMapping("/delivery/{id}")
    public Result delivery(@PathVariable("id") Long id){
        log.info("派送订单，id为：{}",id);
        orderService.delivery(id);
        return Result.success();
    }

    /**
     * 7.完成订单
     * @param id
     * @return
     */
    @ApiOperation("完成订单")
    @PutMapping("/complete/{id}")
    public Result complete(@PathVariable Long id){
        log.info("完成id为：{}的订单",id);
        orderService.complete(id);
        return Result.success();
    }

    /**
     * 8.拒单
     * @param ordersRejectionDTO
     * @return
     */
    @ApiOperation("拒单")
    @PutMapping("/rejection")
    public Result rejection(@RequestBody OrdersRejectionDTO ordersRejectionDTO) throws Exception {
        log.info("拒单：{}",ordersRejectionDTO);
        orderService.rejection(ordersRejectionDTO);
        return Result.success();
    }
}
